import Vue from "vue";
import VueRouter from "vue-router";
import Login from "@/views/login/index.vue";
import Layout from "@/views/layout/index.vue";
import store from "@/store";
import Home from "@/views/home/index.vue";
import Users from "@/views/userManagement/users.vue";
import Rights from "@/views/rightManagement/rights.vue";
import Roles from "@/views/rightManagement/roles.vue";
import Goods from "@/views/merchandising/goods.vue";
import Add from "@/views/merchandising/add.vue";
import Params from "@/views/merchandising/params.vue";
import Categories from "@/views/merchandising/categories.vue";
import Orders from "@/views/orderManagement/orders.vue";
import Reports from "@/views/statistics/reports.vue";

Vue.use(VueRouter);
/**
 * @type {import('vue-router').RouteConfig[]} // 路由配置提示
 */
const routes = [
  {
    path: "/login",
    component: Login,
  },
  {
    path: "/",
    component: Layout,
    redirect: "/home",
    children: [
      {
        path: "home",
        component: Home,
      },
      {
        path: "users",
        component: Users,
      },
      {
        path: "rights",
        component: Rights,
      },
      {
        path: "roles",
        component: Roles,
      },
      {
        path: "goods",
        component: Goods,
      },
      {
        path: "goods/add",
        component: Add,
      },
      {
        path: "params",
        component: Params,
      },
      {
        path: "categories",
        component: Categories,
      },
      {
        path: "orders",
        component: Orders,
      },
      {
        path: "reports",
        component: Reports,
      },
    ],
  },
];

const router = new VueRouter({
  routes,
});
router.beforeEach((to, from, next) => {
  if (to.path !== "/login" && !store.state.user.token) return next("/login");
  next();
});

export default router;
